//
// Created by steven on 2025/9/4.
//
#include "image_converto.h"
#include <cstdio>
#include <cstdlib>
extern "C" {
#include <libavutil/pixfmt.h>
}
int main(int argc, char *argv[])
{
    // 图像尺寸
    const int width = 800;
    const int height = 600;
    const int channel = 3;

    uint8_t *image_host = nullptr;
    size_t image_size = height * width * channel * sizeof(uint8_t);
    image_host = (uint8_t *)aligned_alloc(4096, image_size);
    for (size_t i = 0; i < height * width * channel; i++)
    {
        image_host[i] = (uint8_t)(i % 256);
    }
    uint8_t *image_dev = nullptr;
    rivsMalloc(&image_dev, image_size);
    rivsMemcpy(image_dev, image_host, image_size, rivsMemcpyHostToDevice);

    AVFrame image = AVFrame();
    image.data[0] = image_dev;
    image.format = AV_PIX_FMT_RGB24;
    image.height = height;
    image.width = width;

    // 构造新接口参数
    rivscv::ConvertParam param;
    // 原来的 ROI: top/bottom/left/right -> 新接口: x=left, y=top, width=right, height=bottom
    param.m_srcROI.x = 0;     // left
    param.m_srcROI.y = 0;     // top
    param.m_srcROI.width = 150; // right
    param.m_srcROI.height = 150;// bottom

    param.m_dstROI.x = 0;       // left
    param.m_dstROI.y = 0;       // top
    param.m_dstROI.width = 400; // right
    param.m_dstROI.height = 400;// bottom

    // 与参考实现一致：做通道翻转并按 255.0 归一化
    param.m_swapRGB = true;
    param.m_scaleEnabled = true;
    param.m_scaleAlpha = 1.0/255.0f;

    rivscv::RivsImageTensor out_tensor;
    int ret = rivscv::image_convert_to_L3(&image, &param, &out_tensor);
    if (ret != 0) {
        printf("image_convert_to failed: %d\n", ret);
        rivsFree(image_dev);
        free(image_host);
        return -1;
    }

    int out_width = width - param.m_dstROI.x - param.m_dstROI.width;
    int out_height = height - param.m_dstROI.y - param.m_dstROI.height;
    float *output_host = nullptr;
    size_t out_image_size = static_cast<size_t>(out_height) * out_width * channel * sizeof(float);
    output_host = (float *)aligned_alloc(4096, out_image_size);
    rivsMemcpy(output_host, out_tensor.m_data, out_image_size, rivsMemcpyDeviceToHost);
    printf("===============old data================\n");
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
            printf("%u ", image_host[i]);
        printf("\n");
    }
    int idx = param.m_srcROI.y * width * channel + param.m_srcROI.x * channel;
    printf("origin val=%u  trans value=%f \n", image_host[idx], image_host[idx] / 255.0f);
    printf("===============old data================\n\n");
    printf("===============new data================\n");
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
            printf("%f ", output_host[i]);
        printf("\n");
    }
    printf("compute result=%f\n", output_host[0]);
    printf("===============new data================\n");

    rivsFree(image_dev);
    rivsFree(out_tensor.m_data);
    free(image_host);
    free(output_host);

    return 0;
}
